/*
 *	sndrms.h
 *
 *	Define the RMSWindow structure and its method functions
 */
#ifndef _H_SNDRMS_
#define _H_SNDRMS_

#include <asm/types.h>
#include "run_types.h"
// #include "srvutils/longtime.h"

#define WINDOW_SIZE	25
#define SILENCE_LIMIT	10

typedef struct tagRMSWindow {
	long		silence_threshold;
	long		thresholdx2;
	long		silence_window[ WINDOW_SIZE ];
	long		*window_start;
	long  		*window_end;
	long		*window_ptr;
	int		silence_count;
} RMSWindow;

#define	SNDMODE_IN_FULLDUPLEX		0	// Full duplex input
#define SNDMODE_IN_HALFDUPLEX		1	// Half duplex input
#define SNDMODE_OUT_OSS			2	// output silence suppression
#define SNDMODE_OUT_NOS			3	// output no silence suppression

typedef struct tagDeviceThreshold {
	char			device_path[ 32 ];
	int			device_index;
	/* read end */
	RMSWindow		rms_read;
	_longtime		t_read;			// last time read

	/* write end */
	RMSWindow		rms_write;
	_longtime		t_drain;		// time for last packet to drain (expected)

	/* operation mode */
	int			in_mode;		// input mode )full duplex or half duplex)
	int			out_mode;		// output mode (with or without silence suppression)
	/* reference count */
	int			refer;			// usually shall be 1 or 2

} DeviceThreshold;

extern RMSWindow *RMSW_new( int threshold_init );

/* return
 * 0: silence block
 * 1: non-silent. I/O is required
 */
extern int RMSW_isSilence( RMSWindow *rmsw, short *block, int nsamples, long *rms );

extern DeviceThreshold *SND_getDeviceThreshold( const char *device_name, int device_index, int rw_mode, int op_mode );
extern void SND_destroyDeviceThreshold( DeviceThreshold *dt );

#define RMSW_threshold(rmsw)	( (rmsw)->silence_threshold )

#endif
